package cn.mall.order.interceptor;

import cn.mall.cart.to.UserInfoTo;
import cn.mall.common.constant.AuthServerConstant;
import cn.mall.common.constant.CartConstant;
import cn.mall.common.constant.MemberResponseVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static cn.mall.common.constant.CartConstant.TEMP_USER_COOKIE_NAME;

//用户登录拦截器
@Component//注入到容器当中
public class LoginUserInterceptor implements HandlerInterceptor {

        public static ThreadLocal<UserInfoTo> userKey = new ThreadLocal<>();

        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            UserInfoTo userInfoTo = new UserInfoTo();

            HttpSession session = request.getSession();
            Object attribute1 = session.getAttribute(AuthServerConstant.LOGIN_USER);
            System.out.println("attribute1 = " + attribute1);

            String uri = request.getRequestURI();
            AntPathMatcher antPathMatcher = new AntPathMatcher();
            boolean match = antPathMatcher.match("/order/order/status/**", uri);
            boolean match1 = antPathMatcher.match("/payed/notify", uri);
            if (match || match1) {
                return true;
            }
            //-------------------------------------
            Cookie[] cookies = request.getCookies();
            if (cookies != null && cookies.length > 0) {
                for (Cookie cookie : cookies) {
                    //user-key
                    String name = cookie.getName();
                    if(name.equals("userId")){
                        System.out.println("拿到cookie =userId " + name);

                        userInfoTo.setUserId(Long.valueOf((cookie.getValue())));
                    }
                    if (name.equals(CartConstant.TEMP_USER_COOKIE_NAME)) {
                        userInfoTo.setUserKey(cookie.getValue());
                        //标记为已是临时用户
                        userInfoTo.setTempUser(true);
                    }
                }
            }


            //获取登录的用户信息
            MemberResponseVo attribute = (MemberResponseVo) request.getSession().getAttribute(TEMP_USER_COOKIE_NAME);

            /*if (attribute != null) {*/
                //把登录后用户的信息放在ThreadLocal里面进行保存
                /*userKey.set(attribute);

                return true;*/
            /*}*//* else {
                //未登录，返回登录页面
                *//*response.setContentType("text/html;charset=UTF-8");
                PrintWriter out = response.getWriter();*//*
                *//*out.println("<script>location.href='http://localhost:10013/toTrade'</script>");*//*
                // session.setAttribute("msg", "请先进行登录");
                // response.sendRedirect("http://auth.gulimall.com/login.html");
                userKey.set(attribute);
                return true;
            }*/
            userKey.set(userInfoTo);
            return true;

        }

//        @Override
//        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//
//        }

        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        }
    }
